Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.ApplicationBlocks.Data

Imports DotNetNuke.Common.Utilities
Imports DotNetNuke

Namespace Bring2mind.DNN.Modules.UserFiles.Data

 Public Class SqlDataProvider
  Inherits DataProvider

#Region "Private Members"

  Private Const ProviderType As String = "data"
  Private _providerConfiguration As DotNetNuke.Framework.Providers.ProviderConfiguration = DotNetNuke.Framework.Providers.ProviderConfiguration.GetProviderConfiguration(ProviderType)
  Private _connectionString As String
  Private _providerPath As String
  Private _objectQualifier As String
  Private _databaseOwner As String

#End Region

#Region "Constructors"

  Public Sub New()

   ' Read the configuration specific information for this provider
   Dim objProvider As Framework.Providers.Provider = CType(_providerConfiguration.Providers(_providerConfiguration.DefaultProvider), Framework.Providers.Provider)

   ' Read the attributes for this provider
   _connectionString = Config.GetConnectionString()

   _providerPath = objProvider.Attributes("providerPath")

   _objectQualifier = objProvider.Attributes("objectQualifier")
   If _objectQualifier <> "" And _objectQualifier.EndsWith("_") = False Then
    _objectQualifier += "_"
   End If

   _databaseOwner = objProvider.Attributes("databaseOwner")
   If _databaseOwner <> "" And _databaseOwner.EndsWith(".") = False Then
    _databaseOwner += "."
   End If

  End Sub

#End Region

#Region "Properties"

  Public ReadOnly Property ConnectionString() As String
   Get
    Return _connectionString
   End Get
  End Property

  Public ReadOnly Property ProviderPath() As String
   Get
    Return _providerPath
   End Get
  End Property

  Public ReadOnly Property ObjectQualifier() As String
   Get
    Return _objectQualifier
   End Get
  End Property

  Public ReadOnly Property DatabaseOwner() As String
   Get
    Return _databaseOwner
   End Get
  End Property

#End Region

#Region "Files Methods"
  Public Overrides Function GetFiles(ByVal id As Integer, ByVal portalId As Integer) As IDataReader
   Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "UserFiles_GetFiles", id, portalId), IDataReader)
  End Function


  Public Overrides Function AddFiles(ByVal dateSubmitted As Date, ByVal fileLength As Integer, ByVal fileName As String, ByVal portalId As Integer, ByVal userId As Integer) As Integer
   Return CType(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner & ObjectQualifier & "UserFiles_AddFiles", dateSubmitted, fileLength, fileName, portalId, userId), Integer)
  End Function

  Public Overrides Sub UpdateFiles(ByVal id As Integer, ByVal dateSubmitted As Date, ByVal fileLength As Integer, ByVal fileName As String, ByVal portalId As Integer, ByVal userId As Integer)
   SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "UserFiles_UpdateFiles", id, dateSubmitted, fileLength, fileName, portalId, userId)
  End Sub

  Public Overrides Sub DeleteFiles(ByVal id As Integer)
   SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner & ObjectQualifier & "UserFiles_DeleteFiles", id)
  End Sub
#End Region

#Region "Other Procedures"
  Public Overrides Function GetAllPortalFiles(ByVal PortalId As Integer) As IDataReader
   Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "UserFiles_GetAllPortalFiles", PortalId), IDataReader)
  End Function

  Public Overrides Function GetFileByFilename(ByVal FileName As String, ByVal UserId As Integer, ByVal PortalId As Integer) As IDataReader
   Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "UserFiles_GetFileByFilename", FileName, UserId, PortalId), IDataReader)
  End Function

  Public Overrides Function GetMyFiles(ByVal UserId As Integer, ByVal PortalId As Integer) As IDataReader
   Return CType(SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner & ObjectQualifier & "UserFiles_GetMyFiles", UserId, PortalId), IDataReader)
  End Function

  'converted to executescalar
  Public Overrides Function GetMyTotal(ByVal UserId As Integer, ByVal PortalId As Integer) As Integer
   Return CType(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner & ObjectQualifier & "UserFiles_GetMyTotal", UserId, PortalId), Integer)
  End Function

#End Region

 End Class

End Namespace
